import React, { Component } from "react";
import { connect } from "react-redux";

class CounterClass extends Component {
  render() {
    const { value, increment, decrement } = this.props;
    return (
      <div>
        <h2>类组件 + connect</h2>
        <p>当前值: {value}</p>
        <button onClick={increment}>+1</button>
        <button onClick={decrement}>-1</button>
      </div>
    );
  }
}

// 将 state 映射为组件 props
const mapStateToProps = (state) => ({
  value: state.value,
});

// 将 dispatch 映射为组件方法
const mapDispatchToProps = (dispatch) => ({
  increment: () => dispatch({ type: "counter/incremented" }),
  decrement: () => dispatch({ type: "counter/decremented" }),
});

// 用 connect 包裹类组件
export default connect(mapStateToProps, mapDispatchToProps)(CounterClass);
